package com.gitee.kamismile.stone.basic.data.application.service;

import com.gitee.kamismile.stone.basic.data.application.req.command.res.SysResourcesCreate;
import com.gitee.kamismile.stone.basic.data.application.req.command.res.SysResourcesEdit;
import com.gitee.kamismile.stone.basic.data.application.req.query.res.ResQuery;
import com.gitee.kamismile.stone.basic.data.domain.sysRes.repository.entity.SysResources;
import com.gitee.kamismile.stone.basic.data.domain.sysRes.service.ISysResDomainService;
import com.gitee.kamismile.stone.basic.data.infrastructure.enums.CommonErrorEnum;
import com.gitee.kamismile.stone.basic.data.infrastructure.utils.CriteriaQueryBuilder;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.relational.core.query.Criteria;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;

@Component
public class SysResServiceApplicationService {
    @Autowired
    private ISysResDomainService iSysResDomainService;

    public Mono<SysResources> save(SysResourcesCreate sysResourcesCreate, Integer nTenantId){
        Assert.notNull(nTenantId, CommonErrorEnum.TenantNotFound.getMessage());
        Assert.isTrue(nTenantId==0, CommonErrorEnum.TenantNotOperation.getMessage());
        SysResources sysResources=new SysResources();
        BeanUtils.copyProperties(sysResourcesCreate, sysResources);
        sysResources.setnTenantId(nTenantId);
        return iSysResDomainService.saveOrUpdate(sysResources);
    }
    public Mono<SysResources> update(SysResourcesEdit sysResourcesEdit, Integer nTenantId){
        Assert.notNull(nTenantId, CommonErrorEnum.TenantNotFound.getMessage());
        Assert.isTrue(nTenantId==0, CommonErrorEnum.TenantNotOperation.getMessage());

        return iSysResDomainService.findAndCheckResById(sysResourcesEdit.getnResourcesId(), nTenantId)
                .flatMap(v->{
                    SysResources sysResources=new SysResources();
                    BeanUtils.copyProperties(sysResourcesEdit, sysResources);
                    return   iSysResDomainService.saveOrUpdate(sysResources);
                });
    }

    public Mono<Void> deleteResById(Integer nResourcesId,Integer nTenantId){
        Assert.notNull(nTenantId, CommonErrorEnum.TenantNotFound.getMessage());
        Assert.isTrue(nTenantId==0, CommonErrorEnum.TenantNotOperation.getMessage());

        return iSysResDomainService.findAndCheckResById(nResourcesId, nTenantId)
                .flatMap(v-> iSysResDomainService.deleteSysResById(nResourcesId)
                );
    }

    public Mono<Page<SysResources>> findResPage(ResQuery resQuery){
        Criteria criteria = CriteriaQueryBuilder.builder()
                .eqIgnoreEmpty("nResourcesId", resQuery.getnResourcesId())
                .eqIgnoreEmpty("nParentId", resQuery.getnParentId())
                .likeIgnoreEmpty("sName", resQuery.getsName())
                .create();
        return iSysResDomainService.findResPage(resQuery.getCurrentPage(),resQuery.getPageSize(),criteria);
    }

    public Flux<SysResources> findResById(List<Integer> nResourcesIds){
        if(CollectionUtils.isEmpty(nResourcesIds)){
            return Flux.empty();
        }
        return iSysResDomainService.findResByIds(nResourcesIds);
    }
}
